﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DG.Common.EF.WPF;
using DG.Common.Patterns.MVVM;
using TCC.ControleFluxoDeCaixa.Apresentacao.ViewModels;
using TCC.ControleFluxoDeCaixa.Model.Entities;
using TCC.ControleFluxoDeCaixa.Model.Fluxo;

namespace TCC.ControleFluxoDeCaixa.Apresentacao.Views
{
    /// <summary>
    /// Interaction logic for LancamentoCadastroWindow.xaml
    /// </summary>
    public partial class EmprestimoCadastroWindow : Window
    {
        public EmprestimoCadastroWindow(EmprestimoCadastroWindowViewModel viewModel)
        {
            InitializeComponent();

            if (viewModel == null) throw new ArgumentNullException("viewModel");

            DataContext = viewModel;

            viewModel.Finished += Close;
            viewModel.MessageSent += s => MessageBox.Show(s);
        }

        public static void Inserir(Action<ActionResult<Emprestimo>> fallback)
        {
            var vm = new EmprestimoCadastroWindowViewModel();
            vm.SetAction(FluxoContextFactory.GetInstance().Create(), CRUDOperationType.Create); 
            
            var wnd = new EmprestimoCadastroWindow(vm);

            wnd.Show();

            if (fallback != null)
            {
                vm.Finished += () => fallback(vm.Result);
            }
        }
        public static void Modificar(Emprestimo entity, Action<ActionResult<Emprestimo>> fallback)
        {
            var vm = new EmprestimoCadastroWindowViewModel();
            vm.SetAction(FluxoContextFactory.GetInstance().Create(), CRUDOperationType.Update, entity); 
            var wnd = new EmprestimoCadastroWindow(vm);

            
            wnd.Show();

            if (fallback != null)
            {
                vm.Finished += () => fallback(vm.Result);
            }
        }
        public static void ModificarPagando(Emprestimo entity, Action<ActionResult<Emprestimo>> fallback)
        {
            var vm = new EmprestimoCadastroWindowViewModel();
            vm.SetAction(FluxoContextFactory.GetInstance().Create(), CRUDOperationType.Update, entity);
            var wnd = new EmprestimoCadastroWindow(vm);

            vm.PagamentoRealizado = true;
            wnd.Show();

            if (fallback != null)
            {
                vm.Finished += () => fallback(vm.Result);
            }
        }
        public static void Visualizar(Emprestimo entity, Action<ActionResult<Emprestimo>> fallback)
        {
            var vm = new EmprestimoCadastroWindowViewModel();
            vm.SetAction(FluxoContextFactory.GetInstance().Create(), CRUDOperationType.Retrive, entity); 
            var wnd = new EmprestimoCadastroWindow(vm);

            
            wnd.Show();


            if (fallback != null)
            {
                vm.Finished += () => fallback(vm.Result);
            }
        }
    }
}
